package com.yssystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.yssystem.domain.Role;
import com.yssystem.domain.dto.RoleDTO;
import com.yssystem.service.IRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/roles")
@RequiredArgsConstructor
@CrossOrigin
public class RoleController {
    private final IRoleService iRoleService;


    @GetMapping
    public Page<Role> getList(@RequestParam Integer page,Integer size,String roleName){
        Page<Role> rolePage = iRoleService.lambdaQuery()
                .like(roleName != null, Role::getRoleName, roleName)
                .page(new Page<>(page, size));
        return rolePage;
    }
    @PostMapping
    public boolean save(@RequestBody RoleDTO roleDTO){
        Role role = new Role();
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(roleDTO, role);
        iRoleService.save(role);
        return true;
    }
    @PutMapping
    public boolean update(@RequestBody RoleDTO roleDTO){
        Role role = new Role();
        role.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(roleDTO, role);
        iRoleService.updateById(role);
        return true;
    }

    @DeleteMapping
    public boolean deleteByIds(@RequestParam List<Long> ids){
        iRoleService.removeBatchByIds(ids);
        return true;
    }

}
